package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.User;
import com.okq.util.page.Page;
import com.okq.util.platform.SQLInjection;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 : 用户Dao
 * 作者 : zdl
 * 日期 : 2017/6/28 13:45
 */
@Repository
public class UserDao extends BaseDao {

    public void saveUser(User user) {
        super.save(user);
    }

    public void deleteUser(User user) {
        super.delete(user);
    }

    public void updateUser(User user) {
        super.update(user);
    }

    public void saveOrUpdateUser(User user) {
        super.saveOrUpdate(user);
    }

    public User getUserById(String id) {
        return (User) super.get(User.class, id);
    }

    public List<User> listAllUser() {
        String hql = "FROM User WHERE isDelete=0";
        return super.find(hql);
    }

    /**
     * 根据登录用户查询用户信息
     *
     * @param loginName 用户登录名称
     * @return 用户对象
     */
    public User getUserByLoginName(String loginName) {
        loginName = SQLInjection.TransactSQLInjection(loginName); // 处理特殊字符，防止注入
        String hql = "FROM User WHERE isDelete = 0 AND loginName = '" + loginName + "'";
        return (User) super.get(hql);
    }

    public List<User> listUserByGroupId(String groupId) {
        String hql = "FROM User WHERE isDelete = 0 AND groupId = '" + groupId + "'";
        return super.find(hql);
    }

    public List<User> listUserByRoleId(String roleId) {
        String hql = "FROM User WHERE isDelete = 0 AND roleId = '" + roleId + "'";
        return super.find(hql);
    }

    /**
     * 分页查询User
     *
     * @param page 分页对象
     * @param user 用户查询
     * @return page
     */
    public Page<User> pageByUser(Page<User> page, User user, String groupIds) {
        StringBuilder sb = new StringBuilder();
        sb.append("FROM User u WHERE u.isDelete=0 ");
        if (user != null) {
            Map<String, Object> param = new HashMap<>();
            String userName = user.getUserName();
            if (StringUtils.isNotBlank(userName)) {
                sb.append(" AND u.userName LIKE ?1 ");
                param.put("1", "%" + userName + "%");
            }

            String loginName = user.getLoginName();
            if (StringUtils.isNotBlank(loginName)) {
                sb.append(" AND u.loginName LIKE ?2 ");
                param.put("2", "%" + loginName + "%");
            }

            String groupId = user.getGroupId();
            if (StringUtils.isNotBlank(groupId) && !"-1".equals(groupId)) {
                sb.append(" AND u.groupId = ?3 ");
                param.put("3", groupId);
            } else {
                if (StringUtils.isNotBlank(groupIds)) {
                    sb.append(" AND u.groupId IN (?4) ");
                    param.put("4", groupIds);
                }
            }
            String roleId = user.getRoleId();
            if (StringUtils.isNotBlank(roleId) && !"-1".equals(roleId)) {
                sb.append(" AND u.roleId = ?5 ");
                param.put("5", roleId);
            }
            sb.append(" ORDER BY u.createTime DESC ");
            return super.findByPage(page, sb.toString(), param);
        }
        return page;
    }

    /**
     * 根据人员名称获取人员List
     *
     * @param userName 用户名
     * @return 用户list
     */
    public List<User> listUserByUserName(String userName) {
        StringBuilder sb = new StringBuilder();
        sb.append("FROM User WHERE isDelete=0 ");
        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(userName)) {
            sb.append(" AND userName LIKE ?1 ");
            param.put("1", "%" + userName + "%");
        }
        return super.find(sb.toString(), param);
    }

    /**
     * @param groupIds 部门Id拼接字符串
     * @return 用户list
     */
    public List<User> listUserByGroupIds(String groupIds) {
        if (StringUtils.isNotBlank(groupIds)) {
            String hql = "FROM User u WHERE u.isDelete=0 AND u.groupId IN (" + groupIds + ")";
            return super.find(hql);
        }
        return null;
    }

    /**
     * 根据手机查询人员
     *
     * @param cellPhone 手机号
     * @return 用户
     */
    public User getUserByPhone(String cellPhone) {
        if (StringUtils.isNotBlank(cellPhone)) {
            String hql = "FROM User WHERE isDelete=0 AND cellphone = '" + cellPhone + "'";
            return (User) super.get(hql);
        }
        return null;
    }

}
